package com.googlecode.monitoria.server.command;

import com.googlecode.monitoria.client.beans.User;
import com.googlecode.monitoria.client.beans.UserType;
import com.googlecode.monitoria.client.to.UserTO;
import com.googlecode.monitoria.server.persistence.dao.AbstractDAO.DAOFactory;
import com.googlecode.monitoria.server.persistence.dao.AbstractDAO.UserDAO;
import com.googlecode.monitoria.server.persistence.util.BCrypt;

public class AdministratorCommands implements MonitoriaCommands {

	@Override
	public UserTO createTeacher(String name, String password) throws Exception {
		
		UserDAO userDAO = DAOFactory.getUserDAO();
		if( userDAO.findByID(name) != null){
			throw new Exception("Choose another name.");
		}
		User newTeacher = new User(name, BCrypt.hashpw( password, BCrypt.gensalt() ), UserType.TEACHER);
		userDAO.save( newTeacher );
		return new UserTO(newTeacher);
	}

}
